// app.js

const utils = require('./utils/utils.js');
const sqtools = require("./utils/SqTools.js")
const urls = require("./urls.js")
const common = require("./utils/common")
if (wx) {
    wx.sqtools = sqtools;
    wx.su = utils;
    wx.surls = urls;
    wx.scommon = common;
    
}
App({
    onLaunch() {

        wx.getSystemInfo({
            success: (result) => {
                this.globalData.safeArea = result.safeArea
            },
            fail: (res) => {},
            complete: (res) => {},
        })
    },
    globalData: {
        userInfo: null,
        safeArea: null,
        stores: []
    },
    fetchUserInfo() {
        if (!this.globalData.loginedUserInfo) {
            let userinfo = null;
            try {
                userinfo = sqtools.getsd("SQ_LOGINED_USERINFO_STOREGE");
            } catch (error) {
                return null;
            }
            if (userinfo && userinfo != {}) {
                this.globalData.loginedUserInfo = userinfo;
                return this.globalData.loginedUserInfo;
            } else {
                // relogin();
                return null;
            }


        }
        return this.globalData.loginedUserInfo;
    },
    writeUserInfo(userinfo) {
        let currentSeconds = new Date().getTime();

        userinfo.expiredTime = currentSeconds + 60 * 60 * 24 * 7 * 1000;
        sqtools.setsd("SQ_LOGINED_USERINFO_STOREGE", userinfo);
        this.globalData.loginedUserInfo = userinfo;
        this.userinfo = userinfo;
        // wx.setStorageSync("SQ_LOGINED_USERINFO_STOREGE", JSON.stringify(userinfo));
    },

    get userinfo() {
        if (!this.globalData.loginedUserInfo) {
            let userinfo = null;
            try {
                userinfo = sqtools.getsd("SQ_LOGINED_USERINFO_STOREGE");
            } catch (error) {
                return null;
            }
            if (userinfo && userinfo != {}) {
                this.globalData.loginedUserInfo = userinfo;

                return this.globalData.loginedUserInfo;
            } else {
                // relogin();
                return null;
            }


        }

        return this.globalData.loginedUserInfo;
    },
    set userinfo(userinfo) {
        let currentSeconds = new Date().getTime();
        this.globalData.loginedUserInfo = userinfo;
        userinfo.expiredTime = currentSeconds + 60 * 60 * 24 * 7 * 1000;
        // sqtools.setsd("SQ_LOGINED_USERINFO_STOREGE", userinfo);
    },
    utils: utils,
    sqtools: sqtools,
    getToken() {
        return this.userinfo.token;
    },
})